
#@+leo-ver=5-thin
#@+node:slzatz.20120318160356.1677: * @file logger.py
#@@first
#@@nowrap
#@@tabwidth -4
#@@language python
#@+others
#@+node:slzatz.20120318160356.1678: ** imports
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future_builtins import *

import os

import sip
sip.setapi('QString', 2)  
sip.setapi('QVariant', 2)  

from PyQt4.QtCore import *
from PyQt4.QtGui import QPlainTextEdit, QMessageBox
#@+node:slzatz.20120318160356.1676: ** class Logger
class Logger (QPlainTextEdit): #(QTextEdit):
    def __init__(self, parent=None, logfile=None):
        QPlainTextEdit.__init__(self, parent)

        self.appendPlainText("%s\n"%time.asctime())

        self.path = logfile

        self.setWordWrapMode(QTextOption.NoWrap)

        self.document().setDefaultFont(QFont('Helvetica', 8))

    def write(self, msg):
        # for some reason each print seems to create two writes, one of which is just a line feed
        msg = msg.strip()
        if msg: 
            self.appendPlainText(msg)

    def transfer(self):    
        path = os.path.join(os.environ['TMP'],'logger.%s'%NOTE_EXT)

        f = file(path,'w')
        f.write(self.toPlainText())
        f.close()

        os.startfile(path)

    def save(self):
        f = file(self.path,'a')
        f.write(self.toPlainText())
        f.close()

        QMessageBox.information(self,  'Information', "Appended test to logfile.txt")

    def clear_text(self):
        self.clear()
        self.appendPlainText("%s\n"%time.asctime()) 

    def save_and_clear(self):
        f = file(self.path,'a')
        f.write(self.toPlainText())
        f.close()

        self.clear()
        self.appendPlainText("%s\n"%time.asctime())

        QMessageBox.information(self,  'Information', "Appended test to logfile.txt")


#@-others
#@-leo
